<?php

class m_form_actions_block_AddStructureAction extends m_backoffice_extends_FormModal
{
	public function execute()
	{
        $parent = $this->getDocumentProvider()->getByUniqueId($this->getRequestParameter("docId"), $this->getRequestParameter("docLang"));

        $templates = m_form_services_FormService::getInstance()->getFormTemplates();
        $this->addToModel('templates', $templates);

        if($this->isFormSubmit())
        {
            $formValues = $this->getFormValues('form');

            if(isset($formValues['label']) && $formValues['label'] != '')
            {
                $website = $this->getContextWebsite();
                $structure = m_form_services_FormService::getInstance()->setContextWebsite($website)->getFormStructure($formValues['label'], $formValues['template']);
                $form = m_form_documents_Structure::createNew()->setLang($website->getLang())->setStructure($structure)->setWebsiteId($website->getUniqueId())->setLabel($formValues['label'])->save();
                $parent->addForm($form)->save();
                $this->closeModalAndRedirect($form->getEditUrl());
            }
            else
            {
                $errors['label'] = f_locale_Manager::getLocale('backoffice.errors.required');
                $this->addToModel('errors', $errors);
            }
        }

		$this->setModalParameters();
		$this->setView('success');
	}	
}